import pymysql

HOST= 'localhost'
PORT = 3306
USER = 'root'
PASSWORD = 'root'
DB = 'p9workflow'
CHARSET = 'utf8'
CURSORCLASS=pymysql.cursors.DictCursor

class Mysql():
    def __init__(self):
        #连接数据库
        self.conn = pymysql.connect(
            host=HOST,
            port=PORT,
            user=USER,
            db=DB,
            password=PASSWORD,
            charset=CHARSET,
            cursorclass=CURSORCLASS
        )
        #游标
        self.cousor = self.conn.cursor()

    #添加数据
    def insert(self,sql):
        self.cousor.execute(sql)
        self.conn.commit()  
        return '添加成功'

    #删除数据
    def delete(self,sql):
        self.cousor.execute(sql)
        self.conn.commit()  
        return '删除成功'

    #修改数据
    def update(self,sql):
        self.cousor.execute(sql)
        self.conn.commit()  
        return '修改成功'

    #展示一条数据
    def find_one(self,sql):
        self.cousor.execute(sql)
        res = self.cousor.fetchone()
        return res

    #展示所有数据
    def find_all(self,sql):
        self.cousor.execute(sql)
        res = self.cousor.fetchall()
        return res
    #关闭
    def close(self):
        self.conn.close()
        self.cousor.close()

        
db = Mysql()
#添加测试
# sql = "insert into cate(`id`,`name`) values(1,'日用品') "
# print(db.insert(sql))

#删除测试
# sql = "delete from cate where id=1 "
# print(db.delete(sql))

#测试修改
# sql = "update cate set name='哈哈' where id=2 "
# print(db.update(sql))

# 查看一条数据
# sql = "select * from cate"
# print(db.get_one(sql))

# 查看所有数据
# sql = "select * from cate"
# print(db.get_all(sql))